package com.java.struts.foodtype.action;

import com.java.struts.base.action.BaseAction;
import com.java.struts.foodtype.entity.FoodtypeinfoEntity;
import com.java.struts.foodtype.service.FoodTypeService;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import java.util.List;
import java.util.Map;

public class FoodTypeAction extends BaseAction {
    private FoodTypeService foodTypeService = new FoodTypeService();
    private Integer typeId;
    public Integer getTypeId() {
        return typeId;
    }
    public void setTypeId(Integer typeId) {
        this.typeId = typeId;
    }
    private  FoodtypeinfoEntity foodType = new FoodtypeinfoEntity();
    public FoodtypeinfoEntity getFoodType() {
        return foodType;
    }
    public void setFoodType(FoodtypeinfoEntity foodType) {
        this.foodType = foodType;
    }

    /**
     *  查询所有的菜品类型
     * @return
     */
    @Action(value="typelist")
    public String query(){
        List<FoodtypeinfoEntity> listType = foodTypeService.listType();
        setSuccessResult(listType);
        return "json";
    }

    /**
     * 查询单个菜品类型
     * @return
     */
    @Action(value="typeSingle",results={
            @Result(name="json",type = "dispatcher",location="/editMenuType.jsp")})
    public String querySingle(){
        FoodtypeinfoEntity single = foodTypeService.typeSingle(typeId);
        ActionContext context = ActionContext.getContext();
        Map<String, Object> session = context.getSession();
        session.put("type",single);
        System.out.println(single.gettName());
        return "json";
    }

    /**
     *  添加菜品类型
     * @return
     */
    @Action(value="addType",results = {
            @Result(name="success",location = "/menutype.jsp")
    })
    public String add(){
        foodTypeService.addType(foodType);
        return "success";
    }

    /**.
     * 修改菜品类型
     * @return
     * sadf
     */
    @Action(value="updateType",results = {
            @Result(name="success",location = "/menutype.jsp")
    })
    public String update(){
        foodTypeService.updateType(foodType);
        return "success";
    }

    /**
     *  删除菜品类型
     * @return
     */
    @Action(value="deleteType")
    public String delete(){
        foodTypeService.delType(typeId);
        return "json";
    }
}
